package com.app.mymvp.business.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.app.mymvp.R;
import com.app.mymvp.base.BaseActivity;
import com.app.base.utils.T;
import com.app.mymvp.business.search.SearchActivity;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTouch;


/**
 * Created by HJ on 2016/4/5.
 * 首页Activity
 * 包含3个标签页Fragment
 */
public class MainActivity extends BaseActivity {

    private static final int HOME = 0;
    private static final int SCHEDULE = 1;
    private static final int MINE = 2;
    @BindView(R.id.rl_homehead_location)
    RelativeLayout rlHomeheadLocation;
    @BindView(R.id.ll_homehead_search)
    LinearLayout llHomeheadSearch;
    @BindView(R.id.et_homehead_search)
    EditText etHomeheadSearch;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(android.R.id.tabhost)
    FragmentTabHost tabhost;

    private View[] views = new View[3];
    private ImageView[] tab_imageViews = new ImageView[3];
    private TextView[] tab_textViews = new TextView[3];
    private int[] images = {R.mipmap.main_home, R.mipmap.main_schedule, R.mipmap.main_mine};
    private int[] images_pre = {R.mipmap.main_home_pre, R.mipmap.main_schedule_pre, R.mipmap.main_mine_pre};
    private String[] texts = {"首页", "课程表", "我的"};
    private boolean isExit;
    public static int TAB_INDEX = 0;
    private String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        flag = getIntent().getStringExtra("flag");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.rl_homehead_location, R.id.rl_homehead_notice, R.id.ll_homehead_search})
    void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.rl_homehead_location:
                T.show(this, "定位");
                break;
            case R.id.rl_homehead_notice:
                T.show(this, "消息");
                break;
            case R.id.ll_homehead_search:
                T.show(this, "搜索");
                intent.setClass(this, SearchActivity.class);
                startActivity(intent);
                break;
        }

    }

    @OnTouch(R.id.et_homehead_search)
    boolean onTouch(MotionEvent event) {
        T.show(this, "搜索touch");
        if (event.getAction() == MotionEvent.ACTION_UP) {
            Intent intent = new Intent();
            intent.setClass(this, SearchActivity.class);
            startActivity(intent);
        }
        return true;
    }

    @Override
    public void initView() {
        tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        tabhost.addTab(tabhost.newTabSpec(trans(HOME)).setIndicator(initIndicatorView(HOME)),
                HomeFragment.class, null);
        tabhost.addTab(tabhost.newTabSpec(trans(SCHEDULE)).setIndicator(initIndicatorView(SCHEDULE)),
                HomeFragment.class, null);
        tabhost.addTab(tabhost.newTabSpec(trans(MINE)).setIndicator(initIndicatorView(MINE)),
                MineFragment.class, null);
        tabhost.getTabWidget().setDividerDrawable(null);
        tab_imageViews[HOME].setImageResource(images_pre[HOME]);
        tab_textViews[HOME].setTextColor(getResources().getColor(R.color.colorDeepBlue));
        tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                TAB_INDEX = Integer.parseInt(tabId);
                changeTab(TAB_INDEX);
            }
        });
        llHomeheadSearch.setVisibility(View.VISIBLE);
    }

    /**
     * 初始化tab标签页的View
     *
     * @param index
     * @return
     */
    private View initIndicatorView(int index) {
        views[index] = LayoutInflater.from(this).inflate(R.layout.activity_main_tab, null);
        tab_imageViews[index] = (ImageView) views[index].findViewById(R.id.iv_main_tab);
        tab_textViews[index] = (TextView) views[index].findViewById(R.id.tv_main_tab);
        tab_imageViews[index].setImageResource(images[index]);
        tab_textViews[index].setText(texts[index]);
        return views[index];
    }

    /**
     * 点击tab页替换背景
     *
     * @param index
     */
    private void changeTab(int index) {
        for (int i = 0; i < views.length; i++) {
            tab_imageViews[i].setImageResource(images[i]);
            tab_textViews[i].setTextColor(getResources().getColor(R.color.colorLittleBlack));
        }
        tab_imageViews[index].setImageResource(images_pre[index]);
        tab_textViews[index].setTextColor(getResources().getColor(R.color.colorDeepBlue));
        if (index == HOME) {
            tvHeadTitle.setVisibility(View.GONE);
            llHomeheadSearch.setVisibility(View.VISIBLE);
            etHomeheadSearch.setVisibility(View.VISIBLE);
            rlHomeheadLocation.setVisibility(View.VISIBLE);
        } else {
            tvHeadTitle.setVisibility(View.VISIBLE);
            llHomeheadSearch.setVisibility(View.GONE);
            etHomeheadSearch.setVisibility(View.GONE);
            rlHomeheadLocation.setVisibility(View.INVISIBLE);
        }
    }

    private String trans(int index) {
        return String.valueOf(index);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();
        }
        return false;
    }

    /**
     * 双击退出
     */
    private void exitBy2Click() {
        if (!isExit) {
            isExit = true;
            T.show(this, "再按一次退出程序");
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}